import 'dart:io';

import 'package:flutter_network/oss/oss_utils.dart';
import 'package:flutter_network/service_url.dart';
import 'package:wallet_utils/text_util.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:http_parser/http_parser.dart';
import 'package:dio/dio.dart' as dio_http;
import 'color.dart';
import 'log_util.dart';
import 'package:flutter_network/service_method.dart';
///@author sunyc
///@created 2022/1/17
///功能描述：上传图片，获取url
class GetImageUrlUtils {
  ///拍照上传
  static Future<String?> getTakePhotoPath() async {
    final XFile? image =
        await ImagePicker().pickImage(source: ImageSource.camera);
    String? url = await OssUtils.uploadImage(image?.path);
    return url;
  }

  ///多张相册
  static Future<List<String>?> getMutiGalleryPath(context, int max) async {
    List<AssetEntity>? images = await AssetPicker.pickAssets(context,
        themeColor: ColorManager.colorF65247,
        maxAssets: max,
        // gridCount: 3,
        // pageSize: 120,
        requestType: RequestType.image);
    if (images?.isEmpty ?? true) return null;
    List<String> pathList = [];
    for (var entity in images!) {
      final File? originFile = await entity.originFile;
      if (!TextUtil.isEmpty(originFile?.path)) {
        pathList.add(originFile!.path);
      }
    }
    List<String>? urls = await OssUtils.uploadMultiImage(pathList);

    return urls;
  }

  ///相册
  static Future<String?> getGalleryPath() async {
    XFile? image = await ImagePicker().pickImage(source: ImageSource.gallery);
    String? url = await OssUtils.uploadImage(image?.path);
    return url;
  }

  static Future<String?> getPath(ImageSource source) async {
    final XFile? image =
    await ImagePicker().pickImage(source: source);
    int qualityValue =await Repository.calculateQuality(image?.path);
    String? compressPath = await Repository.compressImage(image!.path, qualityValue);
    bool isCompressFileExist = await File(compressPath ?? "").exists();
    if (!isCompressFileExist) return null;
    LogUtils.d("压缩比例----$qualityValue%"
        "\n图片压缩后地址------${compressPath ?? ""}"
        "\n原文件大小------${await File(image.path).length() / 1024}KB"
    "\n压缩后大小------${await File(compressPath ?? "").length() / 1024}KB");
    return compressPath;
  }

  static Future<String> upLoadImg(File imagePath,String name) async {
    dio_http.Response response;
    if (imagePath == null) {
      ToastUtils.showToast("未选择图片");
      return '';
    }
    String path = imagePath.path;
    var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    dio_http.FormData formdata = dio_http.FormData.fromMap(
      {
        "file": await dio_http.MultipartFile.fromFile(path,
            filename: name, contentType: MediaType('image', name))
      },
    );
    dio_http.BaseOptions option = dio_http.BaseOptions(
        contentType: 'multipart/form-data', responseType: dio_http.ResponseType.plain);
    dio_http.Dio dio = dio_http.Dio(option);
    //application/json
    try {
      response=await dio.post<String>(
          baseUrl+'upload/uploadFile',
          data: formdata);
      if (response.statusCode == 200) {
        LogUtils.d(response.data);
        return  response.data['url'];
      }
    } catch (e) {
      print("e:" + e.toString() + "   head=" + dio.options.headers.toString());
    }
    return  '';
  }
}
